package com.fan.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fan.reggie.common.R;
import com.fan.reggie.dto.SetmealDto;
import com.fan.reggie.entity.Category;
import com.fan.reggie.entity.Dish;
import com.fan.reggie.entity.Setmeal;
import com.fan.reggie.entity.SetmealDish;
import com.fan.reggie.mapper.SetmealMapper;
import com.fan.reggie.mapper.StemealDishMapper;
import com.fan.reggie.service.CategoryService;
import com.fan.reggie.service.SetMealService;
import com.fan.reggie.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        //当name有值时，则为模糊查询
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(name!=null, Setmeal::getName, name);
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setMealService.page(setmealPage, setmealLambdaQueryWrapper);

        //需要根据category_id查询其name
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //进行对象的拷贝。records不拷贝是因为，其records属性与setMealDto中的records属性不一样
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        //单独拷贝records属性，并为DTO的record属性添加category_name值
        List<SetmealDto> list = setmealPage.getRecords().stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            //先拷贝其余数据
            BeanUtils.copyProperties(item, setmealDto);
            Category category = categoryService.getById(item.getCategoryId());
            if(category!=null){
                //赋值name
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(list);
        return R.success(setmealDtoPage);
    }

    @PostMapping
    /*添加套餐
    * */
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息；{}",setmealDto.toString());
        setMealService.saveWithDish(setmealDto);
        return R.success("套餐保存成功");
    }

    /*删除套餐
    * */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        setMealService.removeWithDish(ids);
        return R.success("删除套餐成功");
    }

    /*修改套餐状态
    * */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        log.info("status:{}",status);
        log.info("ids:{}",ids);
        setMealService.updateStatus(status,ids);
        return R.success("修改套餐状态成功");
    }

    @GetMapping("/list")
    /*根据条件查询菜品 #查询参数为非json数据，为键值对时，直接使用实体接收即可，不用使用@RequestBody注解
    * */
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus() );
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setMealService.list(queryWrapper);
        return R.success(setmealList);
    }
}
